<template>
  <el-card shadow="never">
    <!-- 新增刷新 -->
    <ListHeader @create="handleCreate" @refresh="getData"></ListHeader>

    <!-- 表格 -->

    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column label="圖片">
        <template #default="{ row }">
          <el-image
            style="width: 150px; height: 100px"
            :src="row.url"
            :preview-src-list="[row.url]"
            :initial-index="0"
            fit="cover"
            :preview-teleported="true"
          />
        </template>
      </el-table-column>

      <el-table-column prop="title" label="標題" />
      <el-table-column prop="status" label="狀態">
        <template #default="{ row }">
          <el-tag :type="row.status == 1 ? '' : 'danger'">{{
            row.status == 1 ? "正常" : "禁用"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button size="small" text type="primary" @click="handleEdit(row)"
            >修改</el-button
          >

          <el-popconfirm
            title="是否要删除?"
            confirm-button-text="確定"
            cancel-button-text="取消"
            @confirm="handleDelete(row.id)"
          >
            <template #reference>
              <el-button class="!m-0" text type="primary" size="small"
                >删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      class="flex justify-center mt-4"
      @current-change="getData"
      :current-page="curpage"
      :page-size="limit"
    />

    <!-- 提交表单 -->
    <FormDrawer ref="formDrawerRef" @submit="handleSubmit" :title="drawerTitle">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="auto">
        <el-form-item label="標題" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="圖片" prop="url">
          <ChooseImage v-model="form.url" />
        </el-form-item>
        <el-form-item label="狀態" prop="status">
          <el-select v-model="form.status" placeholder="請選擇">
            <el-option label="正常" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-form-item>
      </el-form>
    </FormDrawer>
  </el-card>
</template>

<script setup>
import { ref } from "vue";
import ChooseImage from "~/components/ChooseImage.vue";
import {
  getBanner,
  createBanner,
  updateBanner,
  deleteBanner,
} from "~/api/systems.js";

import FormDrawer from "~/components/FormDrawer.vue";
import ListHeader from "~/components/ListHeader.vue";

import { useInitTable, useInitForm } from "~/composables/useCommon.js";

const {
  searchForm,
  resetSearchForm,
  tableData,
  loading,
  curpage,
  total,
  limit,
  getData,
  handleDelete,
} = useInitTable({
  getList: getBanner,
  delete: deleteBanner,
  //   onGetListSuccess: (res) => {
  //
  //     // (total.value = res.data.total), (tableData.value = res.data.data);
  //   },
});

//表单

const {
  formDrawerRef,
  formRef,
  form,
  rules,
  drawerTitle,
  handleSubmit,
  handleCreate,
  handleEdit,
} = useInitForm({
  form: {
    url: "",
    title: "",
    status: "",
  },
  rules: {
    title: [{ required: true, message: "標題不能为空", trigger: "blur" }],
  },
  getData,
  update: updateBanner,
  create: createBanner,
});
</script>
